﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECONTACT.Bussiness;
using ECONTACT.DataLayer;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using DevComponents.Editors.DateTimeAdv;

namespace ECONTACT.Controller
{
    public class HocSinhCtrl
    {
        HocSinhData m_HocSinhData = new HocSinhData();

        #region Hien thi ComboBox
        public void HienThiComboBox(ComboBoxEx comboBox)
        {
            comboBox.DataSource = m_HocSinhData.LayDsHocSinh();
            comboBox.DisplayMember = "MaHS";
            comboBox.ValueMember = "MaHS";
        }
        #endregion

        #region Do du lieu vao DataGridView
        public void HienThi(DataGridView dGV, BindingNavigator bN)
        {
            BindingSource bS = new BindingSource();

            bS.DataSource = m_HocSinhData.LayDsHocSinh();
            bN.BindingSource = bS;
            dGV.DataSource = bS;
        }

        public void HienThi(DataGridViewX dGV,
                            TextBoxX txtMaHS,
                            TextBoxX txtTenHS,
                            TextBoxX txtGioiTinh,
                            CheckBoxX ckbGTinhNam,
                            CheckBoxX ckbGTinhNu,
                            DateTimeInput dtpNgaySinh,
                            TextBoxX txtNoiSinh,
                            TextBoxX txtDiaChi,
                            ComboBoxEx cmbMaPH)
        {
            BindingSource bS = new BindingSource();
            bS.DataSource = m_HocSinhData.LayDsHocSinh();

            DataTable dT = m_HocSinhData.LayDsHocSinh();
            bool gioiTinh = Convert.ToBoolean(dT.Rows[0]["GioiTinh"]);

            if (gioiTinh)
            {
                ckbGTinhNu.Checked = true;
            }
            else
            {
                ckbGTinhNam.Checked = true;
            }

            txtMaHS.DataBindings.Clear();
            txtMaHS.DataBindings.Add("Text", bS, "MaHS");

            txtTenHS.DataBindings.Clear();
            txtTenHS.DataBindings.Add("Text", bS, "TenHS");

            txtNoiSinh.DataBindings.Clear();
            txtNoiSinh.DataBindings.Add("Text", bS, "NoiSinh");

            txtGioiTinh.DataBindings.Clear();
            txtGioiTinh.DataBindings.Add("Text", bS, "GioiTinh");

            dtpNgaySinh.DataBindings.Clear();
            dtpNgaySinh.DataBindings.Add("Value", bS, "NgaySinh");

            txtDiaChi.DataBindings.Clear();
            txtDiaChi.DataBindings.Add("Text", bS, "DiaChi");

            cmbMaPH.DataBindings.Clear();
            cmbMaPH.DataBindings.Add("SelectedValue", bS, "MaPH");
            //bN.BindingSource = bS;
            dGV.DataSource = bS;
        }
        #endregion

        public void LayDsHocSinhTheoLop(ListViewEx lVXemDSHS, String MaLop)
        {
            lVXemDSHS.Items.Clear();
            DataTable m_DT = m_HocSinhData.LayDsHocSinhTheoLop(MaLop);

            foreach (DataRow row in m_DT.Rows)
            {
                ListViewItem item = new ListViewItem();

                item.SubItems.Add(row["MaHS"].ToString());
                item.SubItems.Add(row["TenHS"].ToString());
                item.SubItems.Add(row["NgaySinh"].ToString());
                item.SubItems.Add(row["NoiSinh"].ToString());
                item.SubItems.Add(row["GioiTinh"].ToString());
                item.SubItems.Add(row["DiaChi"].ToString());
                item.SubItems.Add(row["Ten"].ToString());

                lVXemDSHS.Items.Add(item);
            }
        }

        //public String LayDsHocSinhTheoLop1(String MaLop)
        //{
        //    return m_HocSinhData.LayDsHocSinhTheoLop1(MaLop);
        //}

        public String LuuHocSinh(String TenHS, bool GioiTinh, DateTime NgaySinh, String NoiSinh, String DiaChi, String MaPH)
        {
            return m_HocSinhData.LuuHocSinh(TenHS, GioiTinh, NgaySinh, NoiSinh,DiaChi,MaPH);
        }

        public String UpdateHocSinh(String MaHS, String TenHS, bool GioiTinh, DateTime NgaySinh, String NoiSinh, String DiaChi, String MaPH)
        {
            return m_HocSinhData.UpdateHocSinh(MaHS, TenHS, GioiTinh, NgaySinh, NoiSinh, DiaChi, MaPH);
        }

        public void DeleteHocSinh(String MaHS)
        {
            m_HocSinhData.DeleteHocSinh(MaHS);
        }
    }
}
